package com.becca.cake.controller.recommend;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.becca.cake.core.common.BaseAction;
import com.becca.cake.core.common.PagingBean;
import com.becca.cake.core.exception.CommonExceptionHandle;
import com.becca.cake.model.goods.GoodsRecommendModel;
import com.becca.cake.service.recommend.IRecommendService;
import com.becca.cake.util.JsonUtil;
/**
 * 商品推荐controller
 * @copyright  尊星(上海)信息科技有限公司
 * @author 
 * @date 2016年6月2日 下午7:28:02
 */
@Controller
@RequestMapping("/recommendManager")
public class RecommendAction   extends BaseAction{
    private final static Log logger = LogFactory.getLog(RecommendAction.class);
    
    @Autowired
    private IRecommendService iRecommendService;
    
    /**
     * 商品推荐列表查询
     * @param advertModel
     * @return
     */
    @RequestMapping(value = "/selectRecommends", method = RequestMethod.POST)
    @ResponseBody
    public String selectRecommends(GoodsRecommendModel goodsRecommendModel,String recommendStartDate,String recommendEndDate){
        PagingBean page = getInitPagingBean();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("goodsRecommendModel", goodsRecommendModel);
        map.put("pagingBean", page);
        map.put("recommendStartDate", recommendStartDate);
        map.put("recommendEndDate", recommendEndDate);
        try {
            // stmp1:获取商品推荐数据
            List<GoodsRecommendModel> list = this.iRecommendService.selectRecommend(map);
            // stmp2:获取商品推荐总条数
            Long count = this.iRecommendService.selectRecommendCount(map);
            return JsonUtil.writeObjectPageJSON(list, count);
        } catch (Exception e) {
            logger.error("查询商品推荐信息失败!", e);
            return JsonUtil.returnJsonUnSuccess("查询商品推荐信息失败!");
        }
    }
    
    /**
     * 根据id推荐或取消推荐
     * @param recommendId
     * @param status
     * @param recommendisHot
     * @return
     */
    @RequestMapping(value = "/updateRecommendStatusById", method = RequestMethod.POST)
    @ResponseBody
    public String updateRecommendStatusById(@RequestParam(value = "recommendId", required = false) Integer recommendId,
                                      @RequestParam(value = "status", required = false) Integer status,
                                      @RequestParam(value = "recommendisHot", required = false) Integer recommendisHot) {
        try {
            this.iRecommendService.updateRecommendStatusById(recommendId,status,recommendisHot,this.getUserSeesion().getTrueName());
            return JsonUtil.returnJsonSuccess("操作商品推荐信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
    
    /**
     * 根据id热门或取消热门
     * @param advertId
     * @return
     */
    @RequestMapping(value = "/updRecommendIsHot", method = RequestMethod.POST)
    @ResponseBody
    public String updateRecommendIsHotById(@RequestParam(value = "recommendId", required = false) Integer recommendId,
                                      @RequestParam(value = "recommendisHot", required = false) String recommendisHot) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("recommendId", recommendId);
            paramMap.put("recommendisHot", recommendisHot);
            paramMap.put("userName", this.getUserSeesion().getTrueName());
            this.iRecommendService.updateRecommendIsHotById(paramMap);
            return JsonUtil.returnJsonSuccess("操作商品推荐信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
    
    /**
     * 添加商品推荐
     * @author yellow
     * @return
     */
    @RequestMapping(value = "/insertRecommend", method = RequestMethod.POST)
    @ResponseBody
    public  String insertGoodsRecommend( GoodsRecommendModel goodsRecommendModel) {
        try {
            this.iRecommendService.insertRecommend(goodsRecommendModel,this.getUserSeesion().getTrueName());
            return JsonUtil.returnJsonSuccess("操作推荐信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
    
    /**
     * 批量修改Order排序列
     * @param advertId
     * @return
     */
    @RequestMapping(value = "/updRecommendsOrder", method = RequestMethod.POST)
    @ResponseBody
    public String updRecommendsOrder(@RequestParam(value = "jsonList", required = false)String jsonList) {
        try {
            this.iRecommendService.updateOrders(jsonList);
            return JsonUtil.returnJsonSuccess("修改推荐排序成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
}
